package controller;

import common.BaseServlet;
import service.AddressDAO;
import service.OrderDetailDAO;
import service.ProductDAO;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Address;
import bean.OrderDetail;

/**
 * Servlet implementation class CashierServlet
 */
@WebServlet("/Cashier")
public class CashierServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static AddressDAO adao = new AddressDAO();
	private static OrderDetailDAO oddao = new OrderDetailDAO();
	private static ProductDAO pdao = new ProductDAO();
    /**
     * @see BaseServlet#BaseServlet()
     */
    public CashierServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	public String basefun(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		int addressid=Integer.parseInt(request.getParameter("addressid"));
		System.out.println("addressid:"+addressid);
		Address address=adao.queryByAddressid(addressid).get(0);
		StringBuffer addressStr=new StringBuffer();
		addressStr.append("{\"consignee\":\""+address.getConsignee()+
				"\",\"tel\":\""+address.getTel()+"\",\"province\":\""+
				address.getProvince()+"\",\"city\":\""+address.getCity()+
				"\",\"district\":\""+address.getDistrict()+"\",\"street\":\""+
				address.getStreet()+"\",\"address\":\""+address.getAddress()+"\"}");
		System.out.println("addressStr:"+addressStr);
		request.setAttribute("addressStr", addressStr);
		
		int orderid=Integer.parseInt(request.getParameter("orderid"));
		System.out.println("orderid:"+orderid);
		request.setAttribute("orderid", orderid);
		
		String payPrice=request.getParameter("payPrice");
		System.out.println("payPrice:"+payPrice);
		request.setAttribute("payPrice", payPrice);

		StringBuffer goodsListStr=new StringBuffer();
		goodsListStr.append("[");
		//根据orderid传入购买的商品名称
		List<OrderDetail> odList=oddao.queryByOrderid(orderid);
		for(Iterator<OrderDetail> it=odList.iterator();it.hasNext();) {
			OrderDetail od=it.next();
			String goodsName=pdao.queryByGoodsid(od.getGoodsid()).get(0).getName();
			goodsListStr.append("\""+goodsName+"\",");
		}
		goodsListStr.deleteCharAt(goodsListStr.length()-1);
		goodsListStr.append("]");
		System.out.println("goodsListStr:"+goodsListStr);
		request.setAttribute("goodsListStr", goodsListStr);
		
		Date date=new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
		request.setAttribute("orderTime", sdf.format(date));
        
		
		return "f:jsp/Cashier";
	}

}
